import React from 'react';
import { Link } from 'react-router-dom';
import ModelSelector from '../ModelSelector';
import { useChatContext } from '../../contexts/ChatContext';
import { chatService } from '../../services/api';

const MainLayout = ({ children }) => {
  const { state, dispatch } = useChatContext();
  
  const handleNewChat = async () => {
    try {
      const newConversation = await chatService.createConversation();
      dispatch({ type: 'SET_CONVERSATIONS', payload: [...state.conversations, newConversation] });
      dispatch({ type: 'SET_CURRENT_CONVERSATION', payload: newConversation.id });
      dispatch({ type: 'SET_MESSAGES', payload: [] });
    } catch (error) {
      console.error('创建新对话失败', error);
    }
  };

  return (
    <div className="flex h-screen bg-white">
      {/* 侧边栏 */}
      <div className="w-64 border-r bg-gray-50 flex flex-col">
        <div className="p-4 border-b flex items-center">
          <div className="text-xl font-bold text-purple-600">Qwen</div>
        </div>
        
        <div className="p-4">
          <button 
            onClick={handleNewChat}
            className="w-full bg-purple-100 text-purple-600 px-4 py-2 rounded-md hover:bg-purple-200 transition-colors"
          >
            + 新建对话
          </button>
        </div>
        
        <div className="p-4">
          <div className="mb-2 text-gray-500 text-sm">搜索</div>
          <input
            type="text"
            placeholder="搜索..."
            className="w-full px-3 py-2 border rounded-md"
          />
        </div>
        
        <div className="flex-1 overflow-y-auto p-4">
          <div className="mb-2 text-gray-500 text-sm">所有对话</div>
          <div className="space-y-2">
            {state.conversations.map(conv => (
              <div 
                key={conv.id}
                className={`p-2 hover:bg-gray-200 rounded cursor-pointer ${
                  state.currentConversationId === conv.id ? 'bg-gray-200' : ''
                }`}
                onClick={() => {
                  dispatch({ type: 'SET_CURRENT_CONVERSATION', payload: conv.id });
                  chatService.getMessages(conv.id).then(msgs => {
                    dispatch({ type: 'SET_MESSAGES', payload: msgs });
                  });
                }}
              >
                {conv.title}
              </div>
            ))}
          </div>
        </div>
      </div>
      
      {/* 主内容区 */}
      <div className="flex-1 flex flex-col">
        {/* 顶部栏 */}
        <div className="p-4 border-b flex justify-between items-center">
          <ModelSelector />
          <div className="flex items-center space-x-4">
            <Link to="/settings" className="p-2 hover:bg-gray-100 rounded-full">
              <svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
              </svg>
            </Link>
            <div className="h-8 w-8 rounded-full bg-blue-500 text-white flex items-center justify-center">
              B
            </div>
          </div>
        </div>
        
        {/* 内容区 */}
        <div className="flex-1 overflow-hidden">
          {children}
        </div>
      </div>
    </div>
  );
};

export default MainLayout;